﻿using Forum.BaseController;
using Forum.Model.QueryFilters;
using Forum.Service.Interface;
using Microsoft.AspNetCore.Authorization;

namespace Forum.Controllers
{
    /// <summary>
    /// 照片
    /// </summary>
    [Route("Api/Photo")]
    public class PhotoController : BaseController<IPhotoService>
    {
        public PhotoController(IPhotoService defaultService, ILogger<BaseController<IPhotoService>> logger) : base(defaultService, logger)
        {
        }

        [HttpGet]
        public ApiResponsePaged<Photo> GetList([FromQuery] QueryParameters param)
        {
            var pagedList = DefaultService.GetPagedList(param);

            return new ApiResponsePaged<Photo>
            {
                data = pagedList.ToList(),
                total = pagedList.Total,
                haveMore = pagedList.Total > param.Page * param.PageSize
            };
        }

        [HttpGet("{id}")]
        public ApiResponse<Photo> Get(string id)
        {
            var photo = DefaultService.GetById(id);
            return photo == null ? ApiResponse.NotFound($"图片 {id} 不存在") : new ApiResponse<Photo> { data = photo };
        }

        [Authorize]
        [HttpPost]
        public ApiResponse<Photo> Add([FromForm] PhotoReq dto, IFormFile file)
        {
            var photo = DefaultService.Add(dto, file);

            return !ModelState.IsValid ? ApiResponse.BadRequest() : new ApiResponse<Photo>(photo);
        }

        [HttpGet("Waterfall")]
        public ApiResponse GetWaterfallList()
        {
            var list = DefaultService.GetWaterfallList();

            return ApiResponse.OK("", list);
        }

        /*
        [Authorize]
        [HttpDelete("{id}")]
        public ApiResponse Delete(string id)
        {
            var photo = _photoService.GetById(id);
            if (photo == null) return ApiResponse.NotFound($"图片 {id} 不存在");
            var rows = _photoService.DeleteById(id);
            return rows > 0
                ? ApiResponse.Ok($"deleted {rows} rows.")
                : ApiResponse.Error(Response, "deleting failed.");
        }

        
        /// <summary>
        /// 重建图片库数据（重新扫描每张图片的大小等数据）
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost("[action]")]
        public ApiResponse ReBuildData()
        {
            return ApiResponse.Ok(new
            {
                Rows = _photoService.ReBuildData()
            }, "重建图片库数据");
        }

        /// <summary>
        /// 批量导入图片
        /// </summary>
        /// <returns></returns>
        [Authorize]
        [HttpPost("[action]")]
        public ApiResponse<List<Photo>> BatchImport()
        {
            var result = _photoService.BatchImport();
            return new ApiResponse<List<Photo>>
            {
                Data = result,
                Message = $"成功导入{result.Count}张图片"
            };
        }
        */
    }
}
